# Sustainable Office Designer
# by Fabian Ritter <Fabian.Ritter@live.de>, 2012

require("SOD/tools/CheckingTools")
UI.messagebox "CheckingTools loaded!" if $check_on_startup
require("SOD/ConstructionManager")
UI.messagebox "ConstructionManager loaded!" if $check_on_startup
require("SOD/tools/OpenStudio/Collection")
UI.messagebox "Collection loaded!" if $check_on_startup
require("SOD/model/ModelInterface")
UI.messagebox "ModelInterface loaded!" if $check_on_startup
require("SOD/model/CoreInterface")
UI.messagebox "CoreInterface loaded!" if $check_on_startup

module SOD
  
  class ModelManager
    
    attr_reader :bars, :projectValues, :set_projectValues
    attr_reader :drawVolumes, :drawStructure
    attr_reader :discardModel
    attr_accessor :active_bar
    
    def initialize
      @active_bar = Collection.new
      @projectValues = Hash.new
      
    end
    
    def start
      
      @bars = Collection.new
      @constructionManager = ConstructionManager.new
      @checkingTools = CheckingTools.new
      
    end
    
    def addBar(pts)
      l = @bars.length
      l += 1
      bar = ModelInterface.new("Bar" + l.to_s)
      puts bar.name.to_s + " created!" if $debug
      bar.sketchInfo.update( pts )
      @bars.add bar
      bar.drawFootprint
      
      bar.check_if_intersect?
      
      return bar
    end
    
    def addCore( bar, pts )
      selBar = @bars.find { |b| b.name == bar.name }
      core = CoreInterface.new( pts )
      selBar.cores.add core
      selBar.drawCore
    end
    
    def drawVolumes
      @bars.each { |b| b.drawVolumes }
    end
    
    def drawConstruction
      @bars.each { |b| b.drawConstruction }
    end
    
    def reset_bars
      @bars.each { |b| b.reset }
    end
    
    def removeBar( barName )
      bar = @bars.find { |b| b.name = barName }
      @bars.remove( bar )
      puts @bars.length if $debug
      #@bars.each_index { |i| @bars[i].name = "Bar" + (i+1).to_s }
    end
    
    def discardModel
      @bars.each { |bar| bar.reset }
      ents = Sketchup.active_model.entities
      footprints = ents.find_all { |e| e.layer.name == "Footprints" }
      ents.erase_entities footprints
    end
    
    def update_projectValues
      vc = 0
      mr = 0
      mp = 0
      mq = 0
      aa = 0
      @bars.each do |bar|
        vc += bar.sgHash.bar_hash[:g_r_vc] # volume concrete
        mr += bar.sgHash.bar_hash[:g_r_mr] # mass reinforcement steel
        mp += bar.sgHash.bar_hash[:g_r_mp] # mass profiled sheeting
        mq += bar.sgHash.bar_hash[:g_r_mq] # mass structure steel
        aa += bar.sgHash.bar_hash[:g_r_aa] # area steel coating
      end
      
      puts vc, mr, mp, mq, aa if $debug
      
      @projectValues[:tot_vc] = vc  # volume concrete
      @projectValues[:tot_mr] = mr  # mass reinforcement steel
      @projectValues[:tot_mp] = mp  # mass profiled sheeting
      @projectValues[:tot_mq] = mq  # mass structure steel
      @projectValues[:tot_aa] = aa  # area steel coating
      
    end
    
    def set_projectValues( values )
      @projectValues[:tot_vc] = values[0]  # volume concrete
      @projectValues[:tot_mr] = values[1]  # mass reinforcement steel
      @projectValues[:tot_mp] = values[2]  # mass profiled sheeting
      @projectValues[:tot_mq] = values[3]  # mass structure steel
      @projectValues[:tot_aa] = values[4]  # area steel coating
    end
    
    def intersecting_bars?()
      status = false
      
      bars.each do |bar|
        intersect = @checkingTools.intersect?( bar )
        status = true if intersect
      end
      
      return status
    end
    
  end
  
end
